package com.ff.javamonkey.file;

import java.io.File;

import com.ff.javamonkey.util.ConnectionSettings;
import com.ff.javamonkey.util.Sha1Sum;

/**
 * This class is used to Locate and Verify the given file using the appropriate
 * hash.
 * @author rhama_arya
 */
public class LocatorVerificator {
	// Debugging
	private final String TAG = "LocatorVerificator";
	
	private File localFDCUDbFile;
	
	public LocatorVerificator() {
		
	}
	
	/**
	 * This method looks for a fdcu db file in the local system, making sure that it is available
	 * in the local system.
	 * @return true if yes, there's a fdcu db file in the local system, false otherwise.
	 */
	public boolean fdcuDbLocator() {
		String fdcuDbLocalFile = ConnectionSettings.LOCAL_FDCU_DBARCHIVE +
			ConnectionSettings.FDCU_FILENAME;
		File file = new File(fdcuDbLocalFile);
		boolean exists = file.exists();
		if (exists) {
			localFDCUDbFile = file;
			return true;
		}
		else {
			localFDCUDbFile = null;
			return false;
		}
	}
	
	/**
	 * Similar with the fdcuDbLocators method, this method looks for acrl db files in the local system.
	 * @return true if yes, there are acrl db files in the local system, false otherwise.
	 */
	public boolean acrlDbLocators() {
		return false;
	}
	
	/**
	 * This method takes a file, and hexedHash as the comparator, and verify it.
	 * @param file File to be checked with SHA1SUM.
	 * @param hexedHash Hexed hash to make a comparison with.
	 * @return true if the file passed verification, false otherwise.
	 */
	public boolean verify(File file, String hexedHash) {
		try {
			String hexed = Sha1Sum.SHA1(file).trim();
			hexedHash = hexedHash.trim();
			return (hexed.equalsIgnoreCase(hexedHash));
		}
		catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}
	
	public File getLocalFDCUDbFile() {
		return localFDCUDbFile;
	}
}
